package com.open.cloud.rbac.controller.orgpost;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.entity.orgpost.CoreOrganization;
import com.open.cloud.rbac.object.orgpost.CoreOrganizationDto;
import com.open.cloud.rbac.object.orgpost.CoreOrganizationSelectDto;
import com.open.cloud.rbac.object.orgpost.CoreOrganizationVo;
import com.open.cloud.rbac.service.orgpost.ICoreOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 组织架构 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-部门组织业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/organization")
public class CoreOrganizationController {

    @Resource
    private ICoreOrganizationService organizationService;

    @ApiOperation(value = "创建")
    @PostMapping("/add")
    public ResponseWrapper saveOrgInfo(@RequestBody CoreOrganizationDto obj) {
        JSONObject data = organizationService.createOrganizationInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updateOrgInfo(@RequestBody CoreOrganizationDto obj) {
        JSONObject data = organizationService.updateOrganizationInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deleteOrgInfo(@RequestParam(name = "id") String id) {
        JSONObject data = organizationService.deleteOrganizationInfo(id);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    ResponseWrapper<IPage<CoreOrganization>> selectOrgInfoPage(@RequestBody CoreOrganizationSelectDto orgSelectDto) {
        IPage<CoreOrganization> data = organizationService.selectOrganizationPage(orgSelectDto);
        return ResponseWrapper.success(data);
    }

    @PostMapping("/tree")
    @ApiOperation(value = "查询部门组织树")
    ResponseWrapper<List<CoreOrganizationVo>> selectOrgInfoTree(@RequestBody CoreOrganizationSelectDto orgSelectDto) {
        List<CoreOrganizationVo> data = organizationService.selectOrganizationTree(orgSelectDto);
        return ResponseWrapper.success(data);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询部门组织列表")
    ResponseWrapper<List<CoreOrganization>> selectOrgInfoList(@RequestBody CoreOrganizationSelectDto orgSelectDto) {
        List<CoreOrganization> data = organizationService.selectOrganizationList(orgSelectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "按companyId查询OrgIds")
    @GetMapping("/ids")
    public ResponseWrapper getOrgIdsByCompanyId(@RequestParam(name = "companyId") String companyId) {
        List<String> data = organizationService.selectOrgIdsByCompanyId(companyId);
        return ResponseWrapper.success(data);
    }


}

